package com.example.a2032.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.a2032.R;
import com.example.a2032.Utils.Utils;
import com.example.a2032.entity.Contact;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ContactAdapter extends RecyclerView.Adapter {

    List<Contact> contactList;

    public ContactAdapter(List<Contact> contactList) {
       this.contactList=contactList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ContactViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false));
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ContactViewHolder settingViewHolder=(ContactViewHolder)holder;
        Contact contact=contactList.get(position);
        String name=contact.getName();
        String iconPath=contact.getIconPath();
        String phoneNumber=contact.getPhoneNumber();
        int contactType=contact.getContactType();
        settingViewHolder.tvSettingItem.setText(name);
        int[]resIds=Utils.getAdapterImageArray(holder,R.array.contact_icon_array);
        settingViewHolder.ivSettingItem.setImageResource(resIds[contactType]);
        settingViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(itemClickListener!=null)itemClickListener.onItemClick(contact);
            }
        });
    }

    @Override
    public int getItemCount() {
        return contactList.size();
    }

    static class ContactViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_setting_item)
        ImageView ivSettingItem;
        @BindView(R.id.tv_setting_item)
        TextView tvSettingItem;
        public ContactViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    onItemClickListener itemClickListener;
    public void setOnItemClickListener(onItemClickListener itemClickListener){
        this.itemClickListener=itemClickListener;
    }
    public interface onItemClickListener{
        public void onItemClick(Contact contact);
    }
    public void updateData(List<Contact> contactList){
        this.contactList=contactList;
        notifyDataSetChanged();
    }
}
